package org.example.springboot.dao.impl;

import org.example.springboot.dao.Carousel_figureDAO;
import org.example.springboot.entity.Carousel_figure;
import org.example.springboot.util.JDBCUtils;

import java.util.List;

public class Carousel_figureDAOImpl implements Carousel_figureDAO {

    @Override
    public List<Carousel_figure> carousel_figureLimit(int start, int rows) {
        return JDBCUtils.executeQuery(Carousel_figure.class,"select * from s_carousel_figure limit ?,?",
                start,rows);
    }

    @Override
    public Integer selectCarousel_figureCount() {
        return JDBCUtils.executeCount("select count(*) from s_carousel_figure ");
    }

    @Override
    public List<Carousel_figure> setAll() {
        List<Carousel_figure> carousel_figures = JDBCUtils.executeQuery(Carousel_figure.class,"select * from s_carousel_figure");
        return carousel_figures;
    }

    @Override
    public Carousel_figure selectById(String id) {
        List<Carousel_figure> carousel_figures = JDBCUtils.executeQuery(Carousel_figure.class,"select * from s_carousel_figure where id = ?",id);
        if(carousel_figures.isEmpty()){
            return null;
        }else {
            return carousel_figures.get(0);
        }
    }

    @Override
    public boolean inser(Carousel_figure carousel_figure) {
        int rows = JDBCUtils.executeUpdate("insert into s_carousel_figure(id,url,sequence_num)values(?,?,?)",
                carousel_figure.getId(),carousel_figure.getUrl(),carousel_figure.getSequence_num());
        return rows > 0;
    }

    @Override
    public boolean delete(String id) {
        int rows = JDBCUtils.executeUpdate("delete from s_carousel_figure where id = ?",id);
        return rows > 0;
    }

    @Override
    public boolean update(Carousel_figure carousel_figure) {
        int rows = JDBCUtils.executeUpdate("update s_carousel_figure set url = ?, sequence_num = ? where id = ?",
                carousel_figure.getUrl(),carousel_figure.getSequence_num(),carousel_figure.getId());
        return rows > 0;
    }
}
